import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

// java Client fileNameOfImage
public class Client {

	public static void main(String[] args) throws IOException {
		for (String s: args) {
            System.out.println(s);
        }
		//get file name from input
		String upload_file = "no";
		int numOfArgs = args.length;
		if (args.length <1) 
		{
			System.out.println("Usage: java Client fielName");
			System.exit(0);
		} else {
			upload_file = args[0];
			System.out.println("upload file name is : " + upload_file);
		}
		
		//System.exit(0);
		
		
		final String EOL = "\r\n";
		final String MULTIPART_BOUNDARY = "------------------563i2ndDfv2rTHiSsdfsdbouNdArYfORhxcvxcvefj3q2f";
		final String UPLOADED_FILE_NAME = upload_file;
		//final String UPLOADED_FILE_NAME = "book_01.jpg";   //Always use real name of the file on line 28

		//String url = "http://ec2-184-169-228-174.us-west-1.compute.amazonaws.com:8000/search/";
		String url = "http://ec2-184-169-228-174.us-west-1.compute.amazonaws.com:8080/upload";
		
		File fileToUpload = new File("./images/" + upload_file);

		HttpURLConnection connection = null;
		OutputStream os = null;
		DataInputStream is = null;
		try {
			connection = (HttpURLConnection) new URL(url).openConnection();
			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.connect();
			os = new BufferedOutputStream(connection.getOutputStream());
			if(os != null) {
				os.write(("--" + MULTIPART_BOUNDARY + EOL).getBytes());
				//os.write(String.format("Content-Disposition:form-data;name=\"UploadedFile\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes());
				os.write(String.format("Content-Disposition:form-data;name=\"data\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes());

				// Upload file(s) data here and send

				is  = new DataInputStream(new FileInputStream(fileToUpload));
				int a = 0;
				try {
					while(is.available()>0){
						a++;
						byte c = is.readByte();
						os.write(c);
					}
				} finally {
					if (is != null) try { is.close(); } catch (IOException logOrIgnore) {}
				}
				System.out.println(a+" Bytes uploaded.");




				os.write((EOL + "--" + MULTIPART_BOUNDARY + "--" + EOL + EOL).getBytes());
				os.flush();
				os.close();
				os = null;
			}
			if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				String line = null;
				while((line = in.readLine()) != null) {
				  System.out.println(line);
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			try {
				if(os != null) {
					os.close();
				}
			} catch (IOException e) {
				//Log.e(TAG, "sendFileToServer exception: close OutputStream", e);
			}
			try {
				if(is != null) {
					is.close();
				}
			} catch (IOException e) {
				//Log.e(TAG, "sendFileToServer exception: close InputStream", e);
			}
			if(connection != null) {
				connection.disconnect();
			}
		}



	}

}
